using System;
using System.Collections.Generic;
using System.Configuration;

namespace Framework.Library.Core.Utils.Miscellaneous
{
    public static class ConfigUtils
    {
        /// <summary>
        /// Gets the value from the applications appSettings. This will error if the appSetting is not defined.
        /// </summary>
        /// <param name="appSettingName">Name of the appSettings key.</param>
        /// <returns></returns>
        public static T GetAppSetting<T>(string appSettingName) where T : IConvertible
        {
            return GetAppSettingInternal(appSettingName, false, default(T));
        }

        /// <summary>
        /// Gets the value from the applications appSettings.
        /// </summary>
        /// <param name="appSettingName">Name of the appSettings key.</param>
        /// <param name="defaultValue">The default value returned if the appSetting has not been defined.</param>
        /// <returns></returns>
        public static T GetAppSetting<T>(string appSettingName, T defaultValue) where T : IConvertible
        {
            return GetAppSettingInternal(appSettingName, true, defaultValue);
        }

        private static T GetAppSettingInternal<T>(string appSettingName, bool useDefaultOnUndefined, T defaultValue)
            where T : IConvertible
        {
            string value = ConfigurationManager.AppSettings[appSettingName];

            // require that all appSettings are either defined or have explicitly been given a default value
            if (value == null)
            {
                if (useDefaultOnUndefined)
                    return defaultValue;
                else
                    throw new Exception(string.Format("{0} not defined in appSettings.", appSettingName));
            }

            return (T) Convert.ChangeType(value, typeof (T));
        }

        public static void AddConfigurationProperties(ConfigurationPropertyCollection collection,
                                                      IEnumerable<ConfigurationProperty> properties)
        {
            if (collection == null)
                throw new ArgumentNullException("collection");
            if (properties == null)
                throw new ArgumentNullException("properties");

            foreach (ConfigurationProperty property in properties)
            {
                collection.Add(property);
            }
        }

        public static string GetConnectionString(string connectionStringName)
        {
            if (connectionStringName == null)
                throw new ArgumentNullException("connectionStringName");

            ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings[connectionStringName];

            if (settings == null)
                throw new Exception(string.Format("No connection string settings with the name '{0}'.",
                                                  connectionStringName));

            return settings.ConnectionString;
        }
    }
}